package com.zhan.rkforum.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wujiazhan
 * @className CookieUtil
 * @description TODO
 * @date 2021-06-16 11:07
 */
public class CookieUtil {


    /**
     * 获取cookie值
     *
     * @param request 请求
     * @param name    cookie名字
     * @return {@link String}
     */
    public static String getCookieValue(HttpServletRequest request, String name) {
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空");
        }
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
